package com.zuiacsn.forum.service.impl;

import com.zuiacsn.forum.common.AppResult;
import com.zuiacsn.forum.common.ResultCode;
import com.zuiacsn.forum.dao.MessageMapper;
import com.zuiacsn.forum.exception.ApplicationException;
import com.zuiacsn.forum.model.Message;
import com.zuiacsn.forum.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {

    @Autowired
    MessageMapper messageMapper;
    /**
     * 发送站内信
     * @param message
     */
    public void create(Message message) {
        // 非空校验
        if(message == null || message.getPostUserId() == null
                || message.getReceiveUserId() == null
                || message.getContent() == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验接受者是否存在......

        // 设置默认值
        message.setState((byte) 0); // 0表示未读, 1表示已读
        message.setDeleteState((byte) 0);
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());

        // 更新数据库
        int row = messageMapper.insertSelective(message);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+", 受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }


    /**
     * 查询未读站内信的数量
     * @param receiveUserId
     * @return
     */
    public int selectUnreadCount(Long receiveUserId) {
        // 非空校验
        if(receiveUserId == null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        int count = messageMapper.selectUnreadCount(receiveUserId);
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    /**
     * 更新指定站内信的状态
     * @param id
     * @param state
     */
    public void updateStateById(Long id, Byte state) {
        // 非空校验
        if(id == null || id<=0 || state>2 || state<0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 构造更新对象
        Message updateMessage = new Message();
        updateMessage.setState(state);
        updateMessage.setId(id);
        updateMessage.setUpdateTime(new Date());

        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+", 受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        // 非空校验
        if (repliedId == null || repliedId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验repliedId对应的站内信状态
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        // 更新状态为已回复
        updateStateById(repliedId, (byte) 2);
        // 回复的内容写入数据库
        create(message);
    }

    @Override
    public Message selectById(Long repliedId) {
        // 非空校验
        if (repliedId == null || repliedId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return messageMapper.selectByPrimaryKey(repliedId);
    }


}
